/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.utils;

import java.math.RoundingMode;
import java.text.NumberFormat;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Kenny
 */
public class Double2DecimalRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;
    private int precision;
    private Number numberValue;
    private NumberFormat nf;

    public Double2DecimalRenderer() {
        super();
        setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        precision = 0;
        nf = NumberFormat.getNumberInstance();
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        nf.setRoundingMode(RoundingMode.HALF_UP);
    }

    @Override
    public void setValue(Object value) {
        if ((value != null) && (value instanceof Number)) {
            numberValue = (Number) value;
            value = nf.format(numberValue.doubleValue());
        }
        super.setValue(value);
    }
}
